package com.houtai.eduorder.service.impl;

import com.houtai.commonutils.ordersvo.CourseWebVoOrder;
import com.houtai.commonutils.ordersvo.UcenterMemberOrder;
import com.houtai.eduorder.client.EduClient;
import com.houtai.eduorder.client.UcenterClient;
import com.houtai.eduorder.entity.Order;
import com.houtai.eduorder.mapper.OrderMapper;
import com.houtai.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.houtai.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author xst
 * @since 2022-12-14
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;
    /**
     * 生成订单的方法
     * @param courseId
     * @param memberId
     * @return
     */
    @Override
    public String createOrders(Integer courseId, String memberId) {
        //远程调用，根据用户id获取用户信息
        UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);
        //远程调用，根据课程id获取课程信息
        CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);
        //创建Order对象，向Order对象设置需要的值
        Order order = new Order();
        //设置订单号，用OrderNoUtil随机生成
        order.setOrderNo(OrderNoUtil.getOrderNo());
        //课程号
        order.setCourseId(courseId);
        //课程标题
        order.setCourseTitle(courseInfoOrder.getTitle());
        //课程封面
        order.setCourseCover(courseInfoOrder.getCover());
        //教师名
        order.setTeacherName(courseInfoOrder.getTeacherName());
        //课程价格
        order.setTotalFee(courseInfoOrder.getPrice());

        //用户id
        Integer id = Integer.valueOf(memberId);
        order.setMemberId(id);
        //用户电话
        order.setMobile(userInfoOrder.getMobile());
        //用户昵称
        order.setNickname(userInfoOrder.getNickname());
        //订单状态，0未支付，1支付
        order.setStatus(0);
        //支付方式,微信支付：1
        order.setPayType(1);

        baseMapper.insert(order);

        //返回订单号
        return order.getOrderNo();
    }
}
